﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using MySql.Data.MySqlClient;

namespace Plazos
{
    // La clase FeriadosBD es hija de DBConnect.
    class FeriadosBD : DBConnect
    {

        protected int id_feriados;
        protected int idSem;
        protected String feriado;
       
        private string query;
        private MySqlCommand cmd;
        protected String error = "0";

        public FeriadosBD() : base()
        {

        }

        protected void prueba()
        {
            base.OpenConnection();
            base.CloseConnection();
        }

        // retorna feriado 
    
        public List<Feriados> getFeriad(){
            List<Feriados> feria = new List<Feriados>();
            query = "SELECT * FROM feriado";
            if (OpenConnection())
            {
                cmd = new MySqlCommand(query,connection);
                try
                {
                    MySqlDataReader dr = cmd.ExecuteReader();
                    while (dr.Read())
                    {
                        feria.Add(new Feriados(dr.GetInt32(0), dr.GetInt32(1), dr.GetString(2)));
                    }
                    dr.Close();
                }
                catch (Exception e)
                {
                    Console.WriteLine("["+System.Reflection.MethodBase.GetCurrentMethod().Name+"] " + e.Message);
                }
                finally
                {
                    CloseConnection();
                }
            }
            return feria;

        }



        public bool Insert( int idSem)
        {
            //revisar que no existe..
            query = "SELECT COUNT(*) FROM feriado WHERE feriado = '" + feriado + "';";
            if (this.OpenConnection() == true)
            {
                cmd = new MySqlCommand(query, connection);
                try
                {
                    int cant = Convert.ToInt32(cmd.ExecuteScalar());
                    if (cant > 0)
                    {
                        error = "El feriado que intenta ingresar ya existe en el sistema";
                        CloseConnection();
                        return false;
                    }
                    else
                    {
                        //insertar
                        query = "INSERT INTO feriado (feriado, idSem) VALUES('" + feriado + "','" + idSem + "')";
                        Console.WriteLine(query);
                        //create command and assign the query and connection from the constructor
                        cmd = new MySqlCommand(query, connection);

                        //Execute command
                        try
                        {
                            cmd.ExecuteNonQuery();
                        }
                        catch (Exception e)
                        {
                            Console.WriteLine(e.Message);
                            error = e.Message;
                            this.CloseConnection();
                            return false;
                        }

                        //close connection
                        this.CloseConnection();
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                    error = e.Message;
                    this.CloseConnection();
                    return false;
                }
                return true;
            }
            else
            {
                return false;
            }
        
        }

        public bool eliminar(string feriado)
        {
            query = "DELETE FROM feriado where feriado = '" + feriado + "';";
            if (OpenConnection())
            {
                cmd = new MySqlCommand(query, connection);
                try
                {
                    cmd.ExecuteNonQuery();
                }
                catch (Exception e)
                {
                    Console.WriteLine("[" + System.Reflection.MethodBase.GetCurrentMethod().Name + "] " + e.Message);
                    return false;
                }
                finally
                {
                    CloseConnection();
                }
            }
            return true;
        }
    }
}
